package com.wwwarehouse.common.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.filter.FilterMultipleAdapter;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.bean.filter.SeriesBean;
import com.wwwarehouse.common.eventbus_event.DrawerSeriesCompleteEvent;
import com.wwwarehouse.common.tripartite_widget.mergeadapter.MergeAdapter;

import java.io.Serializable;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Jackie on 2017/8/25.
 * 项目中的抽屉布局(多类型多选)
 */

public class DeskDrawerSeriesFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;
    private ListView mListView;
    private Button mRestBtn;
    private Button mCompleteBtn;

    private List<SeriesBean> mSeriesList;
    private FilterMultipleAdapter[] mFilterMultipleAdapterArray;
    private MergeAdapter mMergeAdapter;

    private static final String KEY_LIST = "key_list";
    private OnDeskDrawerItemClickListener listener;

    public void setOnDeskDrawerItemClickListener(OnDeskDrawerItemClickListener listener){
        this.listener = listener;
    }

    public interface OnDeskDrawerItemClickListener{
        void onDeskItemClick(int pos, View view, boolean isSelected, String title);
    }

    public static final DeskDrawerSeriesFragment newInstance(List<SeriesBean> seriesList) {
        DeskDrawerSeriesFragment deskDrawerSeriesFragment = new DeskDrawerSeriesFragment();

        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_LIST, (Serializable) seriesList);

        deskDrawerSeriesFragment.setArguments(bundle);

        return deskDrawerSeriesFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_desk_drawer_series, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initView() {
        mListView = findView(mRootView, R.id.lv_content);
        mRestBtn = findView(mRootView, R.id.btn_reset);
        mCompleteBtn = findView(mRootView, R.id.btn_complete);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mSeriesList = (List<SeriesBean>) bundle.getSerializable(KEY_LIST);

            mFilterMultipleAdapterArray = new FilterMultipleAdapter[mSeriesList.size()];
            mMergeAdapter = new MergeAdapter();

            for (int i = 0; i < mSeriesList.size(); i++) {
                TextView titleView = (TextView) View.inflate(mActivity, R.layout.item_drawer_filter_series_title, null);
                GridView gridView = (GridView) View.inflate(mActivity, R.layout.item_drawer_filter_series_content, null);

                SeriesBean seriesBean = mSeriesList.get(i);

                titleView.setText(seriesBean.getTitle());

                mFilterMultipleAdapterArray[i] = new FilterMultipleAdapter(mActivity, seriesBean.getList(), seriesBean.isSingle(), seriesBean.getTitle());
                gridView.setNumColumns(seriesBean.getCount());
                gridView.setAdapter(mFilterMultipleAdapterArray[i]);
                mFilterMultipleAdapterArray[i].setOnDrawerItemClick(new FilterMultipleAdapter.OnDrawerItemClick() {
                    @Override
                    public void onClick(int pos, View view, boolean isSelected, String title) {
                        if(listener != null){
                            listener.onDeskItemClick(pos, view, isSelected, title);
                        }
                    }
                });

                mMergeAdapter.addView(titleView);
                mMergeAdapter.addView(gridView);
            }

            //最后加一个空的View，跟底部保持一定距离
            TextView blankView = (TextView) View.inflate(mActivity, R.layout.item_drawer_filter_series_blank, null);
            mMergeAdapter.addView(blankView);

            mListView.setAdapter(mMergeAdapter);
        }
    }

    private void initEvent() {
        mRestBtn.setOnClickListener(this);
        mCompleteBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_reset) {
            for (int i = 0; i < mSeriesList.size(); i++) {

                SeriesBean seriesBean = mSeriesList.get(i);

                List<FilterBean> filterList = seriesBean.getList();

                for (FilterBean filterBean : filterList) {
                    filterBean.setSelect(false);
                }

                mFilterMultipleAdapterArray[i].notifyDataSetChanged();
            }

            EventBus.getDefault().post(new DrawerSeriesCompleteEvent(mSeriesList, true));
        } else if (id == R.id.btn_complete) {
            EventBus.getDefault().post(new DrawerSeriesCompleteEvent(mSeriesList, false));

            ((BaseCardDeskActivity) mActivity).hideDrawerLayout();
        }
    }

    public void notifyDataChanged(int pos){
        if(pos < mFilterMultipleAdapterArray.length){
            FilterMultipleAdapter adapter = mFilterMultipleAdapterArray[pos];
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }
}
